package com.wl.other.controller;

import com.wl.bean.Result;
import com.wl.entity.Message;
import com.wl.other.biz.IMessageBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private IMessageBiz messageBiz;

    @PostMapping("/send")
    public Result sendMessage(@RequestBody Message message) {
        if(message.getSid().equals(message.getRid())) {
            return new Result(0, "不能给自己发送私信", null);
        }
        int result = messageBiz.add(message);
        if (result > 0) {
            return new Result(1,"发送成功",result);
        }else {
            return new Result(0,"发送失败",null);
        }
    }

    @GetMapping("/contacts")
    public Result contacts(Integer userId) {
        List<Integer> contacts = messageBiz.getContactList(userId);
        return new Result(1,"获取成功",contacts);
    }

    @GetMapping("/history")
    public Result getHistory(Integer senderId,Integer receiverId) {
        List<Message> messages = messageBiz.getChatHistory(senderId,receiverId);
        return new Result(1,"获取成功",messages);
    }

    @GetMapping("/unread/count")
    public Result getUnreadCount(Integer userId) {
        int count = messageBiz.getUnreadCount(userId);
        return new Result(1,"获取成功",count);
    }

    @PostMapping("/mark-read")
    public Result markAsRead(@RequestBody List<Integer> messageIds) {
        boolean success = messageBiz.markAsRead(messageIds);
        if (success) {
            return new Result(1,"标记成功",success);
        }else {
            return new Result(0,"标记失败",null);
        }
    }
}
